package pl.edu.pw.polygen.ui.project;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.beans.ProjectBean;
import pl.edu.pw.polygen.core.project.ProjectService;
import pl.edu.pw.polygen.exception.ProjectExistException;
import pl.edu.pw.polygen.ui.explorer.ProjectExplorer;
import pl.edu.pw.polygen.util.ApplicationHelper;

import com.vaadin.ui.AbstractLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Component
@Scope("prototype")
public class ProjectWindow extends Window {

	private static final long serialVersionUID = 3439452761531103410L;
	private static final String windowName = "New Project";

	private Logger LOG = Logger.getLogger(ProjectWindow.class);
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private ProjectForm projectForm;
	
	@Autowired
	private ProjectExplorer projectExplorer;
	
	private ProjectWindow instance;
	private Button okButton;
	private Button cancelButton;
	private HorizontalLayout buttonsLayout;

	public ProjectWindow() {
		super(windowName);
		instance = this;
	}

	@SuppressWarnings("unused")
	@PostConstruct
	private void initialize() {
		setModal(true);
		setResizable(false);
		VerticalLayout layout = (VerticalLayout) this.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
		layout.setSizeUndefined();

		layout.addComponent((AbstractLayout) projectForm);
		layout.addComponent(getButtonsLayout());
		layout.setComponentAlignment(getButtonsLayout(), Alignment.BOTTOM_RIGHT);
	}

	private HorizontalLayout getButtonsLayout() {
		if(buttonsLayout==null){
			buttonsLayout = new HorizontalLayout();
			buttonsLayout.setSpacing(true);
			buttonsLayout.addComponent(getOkButton());
			buttonsLayout.addComponent(getCancelButton());
		}
		return buttonsLayout;
	}

	private Button getOkButton() {
		if (okButton == null) {
			okButton = new Button("Save");
			okButton.addListener(new ClickListener() {

				private static final long serialVersionUID = 982663756216569796L;

				@Override
				public void buttonClick(ClickEvent event) {
					if(projectForm.isValid()){
						String userName = ApplicationHelper.getLoggedUser().getUsername();
						String projectName = projectForm.getProjectName();
						String projectDescription = projectForm.getProjectDescription();
						try{
							ProjectBean projectBean = projectService.createProject(projectName, projectDescription, userName);
							(instance.getParent()).removeWindow(instance);
							projectExplorer.addProjectBean(projectBean);
							ApplicationHelper.showNotification("Save project", "Project '" + projectName +"' has been created successfully.");
						}catch(ProjectExistException e){
							LOG.info("Projet " + projectName + " already exists.");
							ApplicationHelper.showNotification("Save project error", "There was an error while creating a project '" + projectName + "'.");
						}
					}
				}
			});
			okButton.setWidth("80px");
		}
		return okButton;
	}

	private Button getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new Button("Cancel");
			cancelButton.addListener(new ClickListener() {

				private static final long serialVersionUID = 6840259256005300430L;

				@Override
				public void buttonClick(ClickEvent event) {
					(instance.getParent()).removeWindow(instance);
				}
			});
			cancelButton.setWidth("80px");
		}
		return cancelButton;
	}
	
	public void clear(){
		projectForm.setProjectName("");
		projectForm.setProjectDescription("");
	}
	
}
